<template>
  <div class="addUser">
    <div class="header">添加用户</div>
    <div class="form">
      <el-form
        :model="addObj"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addObj.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addObj.email"></el-input>
        </el-form-item>
        <el-form-item>
          <span>状态：</span>
          <el-radio v-model="addObj.status" :label="1">正常</el-radio>
          <el-radio v-model="addObj.status" :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addUser">添加</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { addUser } from "@/api";
export default {
  data() {
    return {
      addObj: {
        username: "",
        email: "",
        status: 1
      },
      radio: "",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 8,
            message: "长度在 3 到 8 个字符",
            trigger: ["blur"]
          }
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  methods: {
    addUser() {
      this.$refs.ruleForm.validate(vaild => {
        if (vaild) {
          var newObj = JSON.parse(JSON.stringify(this.addObj));
          newObj.status = parseInt(newObj.status);
          // console.log(newObj);
          addUser(newObj).then(res => {
            // console.log(res);
            if (res.code && res.msg === "添加成功") {
              this.$router.push({ name: "userindex" });
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.addUser {
  padding: 20px;
  .header {
    height: 50px;
    border-bottom: 1px solid #ccc;
    font-size: 30px;
    font-weight: 900;
    margin-bottom: 100px;
  }
  .form {
    width: 400px;
    height: 300px;
    // background-color: yellow;
    margin: 0 auto;
  }
}
</style>